package com.ruoyi.mobile.service.impl;

import java.util.ArrayList;
import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.mobile.domain.ArticleTagRelation;
import com.ruoyi.mobile.mapper.ArticleTagRelationMapper;
import com.ruoyi.mobile.mapper.TagMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.mobile.mapper.ArticleMapper;
import com.ruoyi.mobile.domain.Article;
import com.ruoyi.mobile.service.IArticleService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 文章Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-09-19
 */
@Service
public class ArticleServiceImpl implements IArticleService 
{
    @Autowired
    private ArticleMapper articleMapper;

    @Autowired
    private TagMapper tagMapper;

    @Autowired
    private ArticleTagRelationMapper articleTagRelationMapper;


    /**
     * 查询文章
     * 
     * @param id 文章主键
     * @return 文章
     */
    @Override
    public Article selectArticleById(Integer id)
    {
        return articleMapper.selectArticleById(id);
    }

    /**
     * 查询文章列表
     * 
     * @param article 文章
     * @return 文章
     */
    @Override
    public List<Article> selectArticleList(Article article)
    {
        return articleMapper.selectArticleList(article);
    }

    /**
     * 新增文章
     * 
     * @param article 文章
     * @return 结果
     */
    @Override
    public int insertArticle(Article article)
    {
        article.setCreateBy(SecurityUtils.getLoginUser().getUserId().toString());
        article.setCreateTime(DateUtils.getNowDate());
        return articleMapper.insertArticle(article);
    }

    /**
     * 修改文章
     * 
     * @param article 文章
     * @return 结果
     */
    @Override
    @Transactional
    public int updateArticle(Article article)
    {
        article.setUpdateBy(SecurityUtils.getLoginUser().getUserId().toString());
        article.setUpdateTime(DateUtils.getNowDate());
        articleMapper.updateArticle(article);
        //先获取现有的文章标签和文章关联信息
        List<Integer> tagIds = tagMapper.selectTagIdsByArticle(article.getId());
        if (!tagIds.equals(article.getTagIds())) {
            articleTagRelationMapper.deleteByArticleId(article.getId());
            List<ArticleTagRelation> list = new ArrayList<>();
            for (Integer tagId : article.getTagIds()) {
                ArticleTagRelation articleTagRelation = new ArticleTagRelation();
                articleTagRelation.setArticleId(article.getId());
                articleTagRelation.setTagId(tagId);
                list.add(articleTagRelation);
            }
            articleTagRelationMapper.insertList(list);
        }
        return 1;
    }

    /**
     * 批量删除文章
     * 
     * @param ids 需要删除的文章主键
     * @return 结果
     */
    @Override
    public int deleteArticleByIds(Integer[] ids)
    {
        return articleMapper.deleteArticleByIds(ids);
    }

    /**
     * 删除文章信息
     * 
     * @param id 文章主键
     * @return 结果
     */
    @Override
    public int deleteArticleById(Integer id)
    {
        return articleMapper.deleteArticleById(id);
    }
}
